﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using KSATouch;

namespace KSACalibrate
{

    /// <summary>
    /// Interaction logic for TouchCalibration.xaml
    /// </summary>
    public partial class DebugForm : Window
    {

        private KSATouch.KSATouch ksaTouch;
        private int KinectID;
        private bool RenderInitialized = false;
        InteropBitmapHelper helper;

        public DebugForm(KSATouch.KSATouch touchHandle, int KinectID)
        {
            InitializeComponent();
            
            this.ksaTouch = touchHandle;
            ksaTouch.FrameReady += new FrameReadyEventHandler(touchHandle_FrameReady);
            this.KinectID = KinectID;
            ksaTouch.DepthManagers[KinectID].Enabled = true;
            ksaTouch.ContactDown += ksaTouch_ContactDown;
            ksaTouch.ContactUp += ksaTouch_ContactUp;
        }

        void ksaTouch_ContactUp(object sender, ContactEventArgs e)
        {
            TouchUpState.Content += "Contact Up: " + e.Contact.Id + "\r\n";
        }

        void ksaTouch_ContactDown(object sender, ContactEventArgs e)
        {
            TouchDownState.Content += "Contact Down: " + e.Contact.Id + " at: "
                + e.Contact.CenterPoint.X + ","
                + e.Contact.CenterPoint.Y 
                + "Size: "
                + e.Contact.Points.Count() + "\r\n";
        }

        void touchHandle_FrameReady(object Sender, FrameReadyEventArgs e)
        {

            DebugCanvas.Children.Clear();
            
            string ContactInfo = "Count: " + ksaTouch.CurrentContacts.Count.ToString() + "\r\n";
            foreach (Contact contact in ksaTouch.CurrentContacts)
            {
                ContactInfo += "ID: " + contact.Id + "\r\n";
                ContactInfo += "Points: " + contact.Points.Count + "\r\n";
                ContactInfo += "Width: " + contact.ContactBlob.BoundingBox.Width + "\r\n";
                ContactInfo += "Height: " + contact.ContactBlob.BoundingBox.Height + "\r\n";
                ContactInfo += "Side: " + contact.ContactBlob.Side + "\r\n";
                ContactInfo += "Lifetime: " + contact.LifeTime + "\r\n";
                ContactInfo += "Blob Lifetime: " + contact.ContactBlob.LifeTime + "\r\n";
                ContactInfo += "X: " + contact.ContactBlob.CenterPoint.X + "\r\n";
                ContactInfo += "Y: " + contact.ContactBlob.CenterPoint.Y + "\r\n";
                //ContactInfo += "Height: " + contact.ContactBlob.CenterPoint.Height + "\r\n";

                if (contact.CenterPoint != null)
                {
                    ContactInfo += "Actual X: " + contact.CenterPoint.X + "\r\n";
                    ContactInfo += "Actual Y: " + contact.CenterPoint.Y + "\r\n";
                }
                ContactInfo += "\r\n";
                
                // render data 
                int i = ksaTouch.DepthManagers[0].DepthBlobs.Count;               
            }
            string hoverData = "";
            foreach (Contact contact in ksaTouch.CurrentHoveringContacts)
            {
                hoverData += "HOVERING: ID: " + contact.Id + "\r\n";
                hoverData += "HOVERING: SIDE: " + contact.ContactBlob.Side + "\r\n";
                hoverData += "HOVERING: Center: " + contact.ContactBlob.CenterPoint + "\r\n";
                hoverData += "HOVERING: Points: " + contact.ContactBlob.Points.Count() + "\r\n";
                hoverData += "HOVERING: Height: " + contact.ContactZHeight + "\r\n";
                hoverData += "HOVERING: YTilt: " + contact.YTilt + "\r\n";
                hoverData += "HOVERING: XTilt: " + contact.XTilt + "\r\n";

            }
            HoverContactState.Content = hoverData;

            byte[] imageGrid = new byte[320 * 240 * 4];
            foreach (KSASupport.DepthBlob blob in ksaTouch.DepthManagers[0].DepthBlobs)
            {
                foreach (Point p in blob.Points)
                {
                    imageGrid[((int)(p.X + (p.Y * 320)) * 4) + 2] = 255;
                    imageGrid[((int)(p.X + (p.Y * 320)) * 4) + 1] = 255;
                    imageGrid[(int)(p.X + (p.Y * 320)) * 4] = 200;
                }

                Point c = blob.CenterPoint;
                imageGrid[(int)((c.X + (c.Y * 320)) * 4)] = 255;

                Rectangle rectangle = new Rectangle()
                {
                    Stroke = Brushes.WhiteSmoke,
                    StrokeThickness = 1,                    
                    Opacity = .3
                };
               
                rectangle.Width = blob.BoundingBox.Width;
                rectangle.Height = blob.BoundingBox.Height;

                //rectangle.Fill = Brushes.Red;
                Canvas.SetLeft(rectangle, blob.BoundingBox.Left);
                Canvas.SetTop(rectangle, blob.BoundingBox.Top);

                DebugCanvas.Children.Add(rectangle);


            }
            int color = 1;
            foreach (KSASupport.DepthBlob blob in ksaTouch.DepthManagers[0].HoveringBlobs)
            {
                foreach (Point p in blob.Points)
                {
                    imageGrid[(int)((p.X + (p.Y * 320)) * 4) + 2] = (byte)(color * 25);
                    imageGrid[(int)((p.X + (p.Y * 320)) * 4) + 1 ] = (byte)(color + 25);
                    imageGrid[(int)((p.X + (p.Y * 320)) * 4)] = (byte)(color * 25);
                }

                Point c = blob.CenterPoint;
                imageGrid[(int)((c.X + (c.Y * 320)) * 4) + 0] = 255;
                imageGrid[(int)((c.X + (c.Y * 320)) * 4) + 1] = 255;
                imageGrid[(int)((c.X + (c.Y * 320)) * 4) + 2] = 255;
                color = color + 3;

                Point edge = blob.EdgePoint;
                imageGrid[(int)((edge.X + (edge.Y * 320)) * 4) + 2] = 255;



                Point end = blob.EndPointGuess;
                imageGrid[(int)((end.X + (end.Y * 320)) * 4) + 2] = 255;

                Rectangle rectangle = new Rectangle() 
                { 
                    Stroke = Brushes.WhiteSmoke,
                    StrokeThickness = 1,
                    Opacity = .3
                };

                rectangle.Width = blob.BoundingBox.Width;
                rectangle.Height = blob.BoundingBox.Height;
                if (blob.Side == KSASupport.BlobTracking.BlobSide.None)
                {
                    rectangle.Stroke = Brushes.LightBlue;
                    rectangle.StrokeThickness = 5;                    
                }
                //rectangle.Fill = Brushes.Red;
                Canvas.SetLeft(rectangle, blob.BoundingBox.Left);
                Canvas.SetTop(rectangle, blob.BoundingBox.Top);

                DebugCanvas.Children.Add(rectangle);
            }


            if (!RenderInitialized)
            {
                helper = new InteropBitmapHelper(320, 240, imageGrid, PixelFormats.Bgr32.BitsPerPixel, PixelFormats.Bgr32);
                DebugRawImage.Source = helper.InteropBitmap;
                RenderInitialized = true;
            }
            else
            {
                helper.UpdateBits(imageGrid);
            }


            
            TouchInfo.Content = ContactInfo;
        }


        private void ExitButton_Click(object sender, RoutedEventArgs e)
        {            
            this.Close();
        }

        private void ClearButton_Click(object sender, RoutedEventArgs e)
        {
            TouchInfo.Content = "";
            TouchDownState.Content = "";
            TouchUpState.Content = "";
        }

        private void ShowBLobsButton_Click(object sender, RoutedEventArgs e)
        {

        }
    }
}
